﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using ProtoGame.GameLib.Entities;
using Microsoft.Xna.Framework.Graphics;

namespace ProtoGame.GameLib.GameComponents
{
    class StorytellingBox<TEnum> : TextBox <TEnum> where TEnum : struct
    {
        protected StorytellingBox(string fontPath, Game game)
            : base(fontPath, game)
        {
        }

        protected override void PlaceWidgets()
        {
            Vector2 dialogDimension = Vector2.Zero;

            if (Illustration == null)
            {
                dialogDimension = Dimension - 2 * WidgetsOffset;
                TextSprite.FitText(Text, dialogDimension.X, dialogDimension.Y);

                // Centraliza no Y e alinha a esquerda o X
                Vector2 textPosition = Position + WidgetsOffset;
                textPosition.Y += ((Dimension.Y - TextSprite.Bounds.Height) / 2) - WidgetsOffset.Y;
                TextSprite.Position = textPosition;
            }
            else
            {
                // Centraliza X e alinha Y ao topo
                Vector2 illusPos = Position + WidgetsOffset;
                illusPos.X += ((Dimension.X - Illustration.Bounds.Width) / 2) - WidgetsOffset.X;
                Illustration.Position = illusPos;

                int illusBottom = Illustration.Bounds.Bottom;
                TextSprite.Position = new Vector2(
                    Position.X + WidgetsOffset.X,
                    illusBottom + WidgetsOffset.Y);

                dialogDimension.X = Dimension.X - 2 * WidgetsOffset.X;
                dialogDimension.Y = Dimension.Y - Illustration.Bounds.Height - 2 * WidgetsOffset.Y;
                TextSprite.FitText(Text, dialogDimension.X, dialogDimension.Y);
            }

            base.PlaceWidgets();
        }

        public void AddStorytelling(GraphicEntity illustration, string text)
        {
            AddFrame(illustration, text);
        }

        public void AddStorytelling(string text)
        {
            AddStorytelling(null, text);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);

            base.Draw(gameTime);

            SpriteBatch.End();
        }

        public static StorytellingBox<TEnum> CreateStorytellingBox(string fontPath, Game game)
        {
            Vector2 viewportDims = new Vector2(game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);

            Rectangle bounds = Rectangle.Empty;
            bounds.X = (int)(viewportDims.X * 0.2f);
            bounds.Y = (int)(viewportDims.Y * 0.1f);
            bounds.Width = (int)(viewportDims.X * 0.8f) - bounds.X;
            bounds.Height = (int)(viewportDims.Y * 0.95f) - bounds.Y;

            StorytellingBox<TEnum> box = new StorytellingBox<TEnum>(fontPath, game);
            box.Initialize();
            box.Position = new Vector2(bounds.X, bounds.Y);
            box.Dimension = new Vector2(bounds.Width, bounds.Height);
            box.WidgetsOffset = Vector2.Zero;

            ScreenStateEffects sse = new ScreenStateEffects();
            sse.StartingTime = TimeSpan.FromSeconds(1);
            sse.EndingTime = TimeSpan.FromSeconds(1);
            box.ScreenState = sse;

            return box;
        }
    }
}
